package com.xgggh.redpack.ui.redpack;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.databinding.DataBindingUtil;

import com.jakewharton.rxbinding3.view.RxView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.xgggh.redpack.R;
import com.xgggh.redpack.base.ui.BaseActivity;
import com.xgggh.redpack.databinding.ActivityOpenRedPackBinding;
import com.xgggh.redpack.domain.app.IntentFlag;
import com.xgggh.redpack.domain.redpack.OpenRedPackConstraint;
import com.xgggh.redpack.domain.redpack.Pack;
import com.xgggh.redpack.ui.redpack.viewbinder.OpenRedPackConstraintViewBinder;
import com.xgggh.redpack.ui.redpack.viewbinder.OpenRedPackViewBinder;
import com.xgggh.redpack.ui.report.CommentActivity;
import com.xgggh.redpack.ui.report.ReportActivity;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.functions.Consumer;

public class OpenRedPackActivity extends BaseActivity {
    ActivityOpenRedPackBinding mBinding;
    Pack mPack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         mPack =getIntent().getParcelableExtra(IntentFlag.data);
        if (mPack==null){
            return;
        }
        mBinding= DataBindingUtil. setContentView( this,R.layout.activity_open_red_pack);
        setSupportActionBar(mBinding.toolbar.toolbar);

        RxView.clicks(mBinding.fab)
                .compose(uiTransformer())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        startActivity(new Intent(OpenRedPackActivity.this,CommentActivity.class));
                    }
                });
       /* RxView.clicks(mBinding.contentOpenRedPack.contentOpenRedPackItem.fee)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        mBinding.contentOpenRedPack.contentOpenRedPackItem.fee.setText("21");
                        Toast.makeText(OpenRedPackActivity.this, "领取成功", Toast.LENGTH_SHORT).show();
                    }
                });*/


        initRecyclerView();
    }

    private void initRecyclerView(){
        //禁用下拉刷新
        mBinding.contentHome.twinklingRefreshLayout.setEnableRefresh(false);
        mBinding.contentHome.twinklingRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                //getRedPack(firstPage());
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                //getRedPack(nextPage());
            }
        });


        adapter.register(Pack.class, new OpenRedPackViewBinder());
        adapter.register(OpenRedPackConstraint.class, new OpenRedPackConstraintViewBinder());

        mBinding.contentHome.recyclerView.setAdapter(adapter);
        //header
        items.add(mPack);
        //constraint
        List list = new ArrayList();
        list.add(new OpenRedPackConstraint("[时间]",1,"2018.12.12 20:00 之前领取"));
        list.add(new OpenRedPackConstraint("[地点]",2,"大陆地区"));
        list.add(new OpenRedPackConstraint("[任务]",3,"找到商家老鸭汤店铺"));
        list.add(new OpenRedPackConstraint("[故事]",4,"获取一份老鸭汤"));

        items.addAll(list);

        adapter.setItems(items);
        adapter.notifyDataSetChanged();

       // getRedPack(firstPage());
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.report, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_report) {
            startActivity(new Intent(this, ReportActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}
